import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../util/ImConstant.dart';
import '../bloc/ImagePageCubit.dart';

class ImagePage extends StatelessWidget {
  final String imagePath;
  final int imageWidth;
  final int imageHeight;
  final int messageId;

  const ImagePage(
      {required this.imagePath,
      required this.imageWidth,
      required this.imageHeight,
      required this.messageId,
      super.key});

  @override
  Widget build(BuildContext context) {
    return _buildInteractivePane(context);
  }

  Widget _buildInteractivePane(BuildContext context) {
    return Scaffold(
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      body: GestureDetector(
        onTap: () {
          Navigator.of(context).pop();
        },
        child: InteractiveViewer(
          constrained: true,
          scaleEnabled: true,
          maxScale: 100,
          minScale: 0.01,
          panEnabled: true,
          child: Center(
            child: Hero(
                tag: 'messageImage_$messageId}',
                child: Image(
                  image: FileImage(File(imagePath)),
                )),
          ),
        ),
      ),
    );
  }

  Widget _buildGesturePane(BuildContext context) {
    var imagePageCubit = context.watch<ImagePageCubit>();
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pop();
      },
      // onPanUpdate: (details) {
      //   imagePageCubit.add(ImagePageMoveEvent(imagePageCubit.state.x + details.delta.dx, imagePageCubit.state.y + details.delta.dy));
      // },
      onScaleUpdate: (ScaleUpdateDetails details) {
        print("scale: ${details.scale}, x: ${details.focalPointDelta.dx}, y: ${details.focalPointDelta.dy}");
        if (details.pointerCount > 1) {
          imagePageCubit.add(ImagePageScaleEvent(details.scale));
        } else {
          imagePageCubit.add(ImagePageMoveEvent(imagePageCubit.state.x + details.focalPointDelta.dx, imagePageCubit
              .state.y + details.focalPointDelta.dy));
        }
      },
      child: Scaffold(
        backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
        body: Stack(
          alignment: AlignmentDirectional.center,
          children: [buildImagePane3(imagePageCubit)],
        ),
      ),
    );
  }

  Widget buildImagePane(ImagePageCubit imagePageCubit) {
    return Positioned(
        left: imagePageCubit.state.x,
        top: imagePageCubit.state.y,
        child: Hero(
            tag: 'messageImage_$messageId}',
            child: Image(
              image: FileImage(File(imagePath)),
            )));
  }

  Widget buildImagePane2(ImagePageCubit imagePageCubit) {
    return Transform.scale(
      scale: imagePageCubit.state.scale,
      child: Positioned(
          left: imagePageCubit.state.x,
          top: imagePageCubit.state.y,
          child: Center(
            child: Hero(
                tag: 'messageImage_$messageId}',
                child: Image(
                  image: FileImage(File(imagePath)),
                )),
          )),
    );
  }

  Widget buildImagePane3(ImagePageCubit imagePageCubit) {
    return Positioned(
        left: imagePageCubit.state.x,
        top: imagePageCubit.state.y,
        child: Transform.scale(
          scale: imagePageCubit.state.scale,
          child: Hero(
              tag: 'messageImage_$messageId}',
              child: Image(
                image: FileImage(File(imagePath)),
              )),
        ));
  }
}
